<template>
	<a-drawer
		:title="formData.id ? '编辑' : '增加'"
		:width="500"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ paddingBottom: '80px' }"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="会员卡号：" name="code">
						<a-input :disabled="formData.id" v-model:value="formData.code" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="当前积分：">
						<a-input :disabled="formData.id" v-model:value="formData.bonus"/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-form-item label="调整类型：" name="actionType">
				<a-radio-group v-model:value="formData.actionType" :options="adjustOptions"></a-radio-group>
			</a-form-item>
			<a-form-item label="调整积分：" name="points">
				<a-input-number :min="0" v-model:value="formData.points" />
			</a-form-item>
			<a-form-item label="积分金额：" name="amount">
				<a-input-number :min="0" v-model:value="formData.amount" />
			</a-form-item>
			<a-form-item label="调整原因：" name="reason">
				<a-select
					v-model:value="formData.reason"
					:options="reasonOptions"
					allow-clear
					placeholder="请选择调整原因"></a-select>
			</a-form-item>
			<a-form-item label="操作组织：" name="storeCode">
				<a-select
					v-model:value="formData.storeCode"
					:options="storeOptions"
					allow-clear
					placeholder="请选择操作组织"></a-select>
			</a-form-item>
			<a-form-item label="操作备注：" name="note">
				<a-textarea type="textarea" v-model:value="formData.note" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
		<Icon-selector ref="iconSelector" @iconCallBack="iconCallBack" />
	</a-drawer>
</template>

<script setup name="crmMemberAdjustBonusForm">
	import { message } from 'ant-design-vue'
	import {required} from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import crmApi from '@/api/crm/crmApi'
	import { getCurrentInstance } from 'vue'
	import tool from "@/utils/tool";
	// 默认是关闭状态
	let visible = $ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	const iconSelector = ref()
	// 打开抽屉
	const onOpen = (record) => {
		visible = true
		formData.value = {
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
			formData.value.cardType += ''
			formData.value.status += ''
			formData.value.points = 0
			formData.value.amount = 0
			formData.value.actionType = 'MINUS'
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible = false
	}

	// 默认要校验的
	const formRules = {
		points: [required('请输入调整积分')],
		reason: [required('请选择调整原因')],
		code: [required('请输入卡号')],
		actionType: [required('请选择操作类型')],
		recharge: [required('请输入本金，精确到小数点后两位')],
		present: [required('请输入赠送金，精确到小数点后两位')]
	}

	// 图标选择器回调
	const iconCallBack = (value) => {
		formData.value.icon = value
	}
	const { proxy } = getCurrentInstance()

	let reasonOptions = proxy.$TOOL
		.dictTypeList('ADJUST_POINT_REASON')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})

	// 性别
	const adjustOptions = proxy.$TOOL.dictTypeList('ADJUST_ACCOUNT_ACTION').map((item) => {
		return {
			value: item['dictValue'],
			label: item['name']
		}
	})

	let storeOptions = tool
		.tableList('ORG_CATEGORY')
		// .filter(item => item.category === 'STORE')
		.map(item => {
			return {
				value: item['code'],
				label: item['name']
			}
		})
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				const param = Object.assign({}, formData.value)
				param.cardCode = param.code
				param.cardId = param.id
				if (param.points === 0) {
					message.error('调整积分不能为0')
				} else {
					submitLoading.value = true
					crmApi.adjustMemberBonusSubmitForm(param, param.id).then(() => {
						visible = false
						emit('successful')
					}).finally(() => {
						submitLoading.value = false
					})
				}

			})
	}
	const parameterChanges = (data) => {
		return data
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
